package com.bigshen.springcloud.orderserviceprovider.service.impl;

import com.bigshen.springcloud.api.R;
import com.bigshen.springcloud.clients.IGoodsFeignClient;
import com.bigshen.springcloud.exception.BizException;
import com.bigshen.springcloud.orderserviceprovider.controller.dto.ItemDto;
import com.bigshen.springcloud.orderserviceprovider.controller.dto.OrderDto;
import com.bigshen.springcloud.orderserviceprovider.convert.OrderConvert;
import com.bigshen.springcloud.orderserviceprovider.mapper.entitys.TbOrder;
import com.bigshen.springcloud.orderserviceprovider.mapper.entitys.TbOrderItem;
import com.bigshen.springcloud.orderserviceprovider.mapper.persistence.TbOrderItemMapper;
import com.bigshen.springcloud.orderserviceprovider.mapper.persistence.TbOrderMapper;
import com.bigshen.springcloud.orderserviceprovider.service.IOrderService;
import com.bigshen.springcloud.vo.ItemDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class IOrderServiceImpl implements IOrderService {

    @Autowired
    private IGoodsFeignClient goodsFeignClient;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @Autowired
    private OrderConvert orderConvert;

    @Override
    @Transactional
    public String createOrder(OrderDto orderDto) {
        // 下单、传入多个{商品id，商品数量}
        /**
         * 1. 锁库存
         * 2. 查询商品信息
         * 3. 创建订单
         */

        // 1. 锁库存
        R<List<ItemDetailVo>> r = goodsFeignClient.decreaseStock(orderConvert.itemsDto2StockDtoList(orderDto.getItems()));
        if (r.getCode() != 200) {
            throw new BizException(r.getMsg());
        }

        // 2. 查询商品信息
        List<Long> ids = orderDto.getItems().stream().map(dto -> Long.parseLong(dto.getItemId()) ).collect(Collectors.toList());
        R<List<ItemDetailVo>> listR = goodsFeignClient.getItemsByIds(ids);

        // 3. 创建订单
        BigDecimal totalPrice = new BigDecimal(0);
        String orderId = UUID.randomUUID().toString().replace("-", "");
        for (ItemDto itemDto : orderDto.getItems()) {  // 下单商品
            for (ItemDetailVo itemDetailVo : listR.getData()) {  // 商品明细对应订单明细item
                if(itemDetailVo.getId().toString().equals(itemDto.getItemId())) {
                    BigDecimal totalFee = itemDetailVo.getPrice().multiply(BigDecimal.valueOf(itemDto.getNum()));
                    totalPrice = totalPrice.add(totalFee);

                    TbOrderItem tbOrderItem=new TbOrderItem();
                    tbOrderItem.setItemId(itemDetailVo.getId());
                    tbOrderItem.setNum(itemDetailVo.getNum());
                    tbOrderItem.setCreateTime(new Date());
                    tbOrderItem.setOrderId(orderId);  // 关联总的订单id
                    tbOrderItem.setPicPath(itemDetailVo.getImage());
                    tbOrderItem.setPrice(itemDetailVo.getPrice());
                    tbOrderItem.setTotalFee(totalFee);
                    tbOrderItem.setStatus(1);
                    tbOrderItem.setTitle(itemDetailVo.getTitle());
                    tbOrderItem.setId(UUID.randomUUID().toString().replace("-",""));
                    tbOrderItemMapper.insert(tbOrderItem);
                }
            }
        }

        // 总订单
        TbOrder tbOrder = new TbOrder();
        tbOrder.setOrderId(orderId);
        tbOrder.setPayment(totalPrice);
        tbOrder.setPaymentTime(new Date());
        tbOrder.setStatus(0);
        tbOrder.setCreateTime(new Date());
        tbOrder.setUpdateTime(new Date());
        tbOrder.setUserId(1000000l);
        tbOrder.setOrderId(orderId);
        tbOrderMapper.insert(tbOrder);

        return orderId;
    }

}
